// pages/index/index.js
var e = getApp(), t = require("../../../utils/formate.js"), a = getApp().getAccessToken, i = getApp().getPromission, s = getApp().getLocationTypeText, n = getApp().getStatusText, c = getApp().getAccStatusText;
var session = require('../../../utils/session.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFuwuhaoHidden: !1,
    hidden: !0,
    lat: 23.099994,
    lng: 113.32452,
    sign: !1,
    contentHeight: "42vh",
    mapHeight: "50vh",
    gpstime: "",
    adress: "",
    scale: 16,
    markers: [{},{}],
    top: "90%",
    controls: [],
    imei: "",
    deviceName: "",
    device: {},
    firstLoad: !0,
    image_prefix: e.system.image_prefix,
    baseColor: e.system.color,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    wx.setNavigationBarTitle({
      title: e.system.app_name
    })

    let vehicleId = options.vehicleId
    if (vehicleId){
      this.setData({
        imei: vehicleId
      })
    }

    this.setMarksers()

    wx.getUserInfo({
      success: function (e) {
        var t = JSON.parse(e.rawData);
        wx.setStorageSync("avatarUrl", t.avatarUrl), wx.setStorageSync("nickName", t.nickName);
      }
    })

    let car = session.currentCar() 
    if(car){
      this.setData({
        device: JSON.parse(car)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mapCtx = wx.createMapContext('map')

    wx.showShareMenu({
      withShareTicket: !0
    })

    this.initMap()

    wx.getLocation({
      type: "gcj02",
      success: function (e) {
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.setTime = setInterval(() => {
      that.setMarksers()
    }, 10000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.setTime);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.setTime);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: e.system.app_name,
      path: " /pages/login/login",
      success: function (e) { },
      fail: function (e) { }
    };
  },

  /*****************************Action****************************/
  navigateToFollow: function (e) {
    wx.navigateTo({
      url: "../follow/follow"
    });
  },

  trace: function () {
    return
    
    var t = this.data.device;
    t ? wx.getSetting({
      success: function (e) {
        e.authSetting["scope.userLocation"] ? wx.navigateTo({
          url: "../trace/trace?imei=" + t.imei
        }) : wx.openSetting({
          success: function (e) { }
        });
      }
    }) : wx.showToast({
      title: "请选择设备",
      image: "/images/" + e.system.image_prefix + "/err.png",
      duration: 1e3
    })
  },

  markertap: function (e) {
    var t = this, a = this.data.isFuwuhaoHidden;
    this.setData({
      sign: !0,
      mapHeight: a ? "66vh" : "62vh",
      contentHeight: a ? "34vh" : "38vh"
    }, function () {
      t.initMap();
    });
  },

  setMarksers: function () {
    let that = this
    let openId = wx.getStorageSync("wechatOpenid")
    let url = this.data.imei 
      ? e.system.app_url_prefix + "/weapp/transport/homePage?openId=" + openId + "&id=" + this.data.imei 
    : e.system.app_url_prefix + "/weapp/transport/homePage?openId=" + openId
    wx.request({
      url: url,
      method: "POST",
      data: {},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (resp) {
        let code = parseInt(resp.data.code)
        if (code == 0) {
          let data = resp.data.data
          console.log(data)
          let carLocation = {
            "id": 0,
            "latitude": data.lat, "longitude": data.lng, "iconPath": "/images/" + e.system.image_prefix + "/new_off_line.png", "width": 30, "height": 30, anchor: {
              x: .5,
              y: .8
            }
          }

          let u = that.data.markers
          u[1] = carLocation
          session.setCurrentCar(JSON.stringify(data))

          that.setData({
            markers: u,
            device: data,
            lat: data.lat,
            lng: data.lng,
          })
        }
      },
      fail: function (resp) { },
      complete: function (resp) {
        wx.hideLoading();
      }
    })
  },

  clearInfo: function (e) {
    var t = this.data.isFuwuhaoHidden;
    this.setData({
      sign: !1,
      mapHeight: t ? "100vh" : "96vh",
      contentHeight: t ? "0vh" : "4vh"
    }, this.initMap);
  },

  controltap: function (t) {
    var s = this, n = this, c = n.data, o = c.markers, r = c.controls, u = c.device;
    if (1 == t.controlId) {
      wx.getLocation({
        type: "gcj02",
        success: function (t) {
          var a = t.latitude, i = t.longitude;
          o[0] = {
            id: 10,
            iconPath: "/images/" + e.system.image_prefix + "/mine.png",
            latitude: a,
            longitude: i,
            width: 30.86,
            height: 30.86,
            anchor: {
              x: .5,
              y: .1
            }
          }, r[0].id = 2, r[0].iconPath = "/images/" + e.system.image_prefix + "/position_car.png",
            n.setData({
              controls: r,
              lat: a,
              lng: i,
              markers: o
            });
        },
        fail: function () {
          i();
        }
      })
    } else if (2 == t.controlId){
      (r = this.data.controls)[0].id = 1, r[0].iconPath = "/images/" + e.system.image_prefix + "/position_my.png",
        this.setData({
          controls: r
        }), this.setData({
          firstLoad: !0
        }, function () {
          s.setMarksers()
        })
    } else if (3 == t.controlId) {
        var d = JSON.stringify(u);
        wx.navigateTo({
          url: "/pages/defence/defence?device=" + d
        });
      } else if (7 == t.controlId) {
        h = this.data.scale;
        n.setData({
          scale: --h
        });
      } else if (8 == t.controlId) {
        var h = this.data.scale;
        n.setData({
          scale: ++h
        });
      }
  },

  regionchange: function () { },

  setClipboardData: function (e) {
    var t = e.currentTarget.dataset.content;
    wx.setClipboardData({
      data: t,
      success: function (e) { }
    });
  },

  getPersonalInfo: function () {
    var t = this;
    let openId = wx.getStorageSync("wechatOpenid")
    wx.request({
      url: e.system.app_url_prefix + "/weapp/transport/homePage?openId=" + openId,
      method: "GET",
      data: {},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        let code = parseInt(e.data.code)
        if (code == 0) {
          t.setInterval()
        }
      },
      fail: function (e) { },
      complete: function (e) { }
    });
  },

  deviceNameTap: function (e) {
    clearInterval(this.setTime);
  },

  /*******************************Func***************************/
  initMap: function () {
    var t = this, a = this.data.isFuwuhaoHidden;
    wx.createSelectorQuery().select("#map").boundingClientRect(function (i) {
      var s = t.data, n = s.controls;
      s.sign;
      n[0] = {
        id: t.id ? t.id : 1,
        iconPath: t.imgUrl ? t.imgUrl : "/images/" + e.system.image_prefix + "/position_my.png",
        position: {
          left: 13,
          top: a ? i.height - 73 : i.height - 103,
          width: 46,
          height: 46
        },
        clickable: !0
      }, n[1] = {
        id: 7,
        iconPath: "/images/" + e.system.image_prefix + "/shrik.png",
        position: {
          left: i.width - 43,
          top: a ? i.height - 63 : i.height - 93,
          width: 31,
          height: 36
        },
        clickable: !0
      }, n[2] = {
        id: 8,
        iconPath: "/images/" + e.system.image_prefix + "/magnify.png",
        position: {
          left: i.width - 43,
          top: a ? i.height - 97 : i.height - 127,
          width: 31,
          height: 36
        },
        clickable: !0
      }, n[3] = {
        id: 3,
        iconPath: "/images/" + e.system.image_prefix + "/defence.png",
        position: {
          left: i.width - 53,
          top: 36,
          width: 46,
          height: 46
        },
        clickable: !0
      }, t.setData({
        controls: n
      });
    }).exec();
  },
  
  setInterval: function (e) {
    function t() {
      return e.apply(this, arguments);
    }
    return t.toString = function () {
      return e.toString();
    }, t;
  }
  
  (function () {
    var e = this;
    e.setData({
      firstLoad: !0
    }, function () {
      e.setMarksers()
    }), this.setTime = setInterval(function () {
      e.setData({
        firstLoad: !1
      }, function () {
        e.setMarksers()
      });
    }, 10000);
  }),

  bindupdated: function (e) { },

  bindtap: function (e) { },

  changeDeviceName: function (t) {
    var i = this, s = this.data.device, n = this;
    t.detail.value ? s.deviceName != t.detail.value && (s.name = t.detail.value,
      this.setData({
        device: s
      }, function () {
        i._changeDeviceName()
      })) : wx.showToast({
        title: "名称不能为空",
        image: "/images/" + e.system.image_prefix + "/err.png",
        duration: 1e3,
        complete: function (e) {
          n.setData({
            device: s
          }, function () {
            n.setMarksers();
          });
        }
      });
  },

  _changeDeviceName: function () {
    var t = this, a = this.data.device;
    wx.request({
      url: e.system.app_url_prefix + "/weapp/transport/homePageUpName?vehicleId=" + a.vehicleId + "&name=" + a.name,
      method: "PUT",
      data: {},
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        let code = parseInt(e.data.code)
        if(code == 0){
          wx.showToast({
            title: "设备名称修改成功",
            icon: "success",
            duration: 1e3
          });
        }
      },
      fail: function (e) { },
      complete: function (e) {
        t.setMarksers()
      }
    });
  }
})